﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-12 17:33:39
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;


namespace OF.Component.DAL
{
	
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class AuthorityUserRoleRela
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个
    	/// </summary>
        public AuthorityUserRoleRelaInfo QuerySingle(Expression<Func<AuthorityUserRoleRelaInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取列表
    	/// </summary>
	    public IList<AuthorityUserRoleRelaInfo> QueryList(Expression<Func<AuthorityUserRoleRelaInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        /// 删除用户角色       
        /// </summary>
        /// <param name="RoleID"></param>
        public void DeleteUserRole(int UserID)
        {
            string strSql = @"	  UPDATE [AuthorityUserRoleRela] SET IsActive=0,UpdatedDate=GETDATE()
                                  WHERE [UserID]=@UserID ";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            //设置参数
            db.AddInParameter(dbc, "@UserID", DbType.Int32, UserID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取用户角色名称
        /// </summary>
        /// <param name="intUserList"></param>
        /// <returns></returns>
        public Dictionary<int, List<string>> GetUserRoleListNameByUserIds(List<int> intUserList)
        {
            Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();
            if (intUserList.Count==0)
            {
                return dic;
            }
            string intUserId = string.Join(",", intUserList.ToArray());
           
            List<string> roleNameList = null;
            string strSql = @"SELECT rel.UserID,ro.RoleName
                              FROM [AuthorityUserRoleRela] rel with(nolock)
                              inner join AuthorityRole ro with(nolock) on rel.[RoleID]=ro.RoleID
                              where rel.isactive=1 and ro.IsActive=1 and rel.[UserID] in("+ intUserId + ") ";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            //设置参数
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    int userId = (int)dr["UserID"];
                    if (!dic.ContainsKey(userId))
                    {
                        roleNameList = new List<string>();
                        roleNameList.Add(dr["RoleName"].ToString());
                        dic.Add(userId, roleNameList);
                    }
                    else
                    {
                        List<string> li = (List<string>)dic[userId];
                        li.Add(dr["RoleName"].ToString());
                        dic[userId] = li;
                    }
                }
            }
            return dic;
        }

    }
	
}